package com.neusoft.neuiotms.dcam.fixed.service.impl;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.neuiotms.dcam.anhui.itsm.WorkflowBean;
import com.neusoft.neuiotms.dcam.fixed.service.ItsmService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ItsmServiceImpl implements ItsmService {

    @Value("${itsm.enabled:false}")
    private boolean itsmEnabled;
    @Value("${itsm.wsdlUrl:http://10.0.148.153:8080/itsm/services/ahnxExternWs?wsdl}")
    private String itsmWsdlUrl;

    @Override
    public void pushItsm(WorkflowBean workflowBean) {
        if (this.itsmEnabled) {
            final JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
            final Client client = dcf.createClient(this.itsmWsdlUrl);
            final HTTPConduit conduit = (HTTPConduit) client.getConduit();
            final HTTPClientPolicy policy = new HTTPClientPolicy();
            policy.setConnectionTimeout(2000);
            policy.setReceiveTimeout(10000);
            conduit.setClient(policy);

            Object[] objects = new Object[0];
            try {
                final String content = JsonUtils.pojoToJson(workflowBean);
                ItsmServiceImpl.log.info("ITSM：发送审批数据: {}", content);
                objects = client.invoke("createAssetApprove", content);
                ItsmServiceImpl.log.info("ITSM：返回数据: {}", JsonUtils.pojoToJson(objects[0]));
            } catch (final java.lang.Exception e) {
                ItsmServiceImpl.log.error("", e);
            }
        } else {
            ItsmServiceImpl.log.info("ITSM：{}", JsonUtils.pojoToJson(workflowBean));
        }
    }
}
